﻿using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversalBlazor.CoreComponent.TabComponent;
using System.Reflection.Metadata.Ecma335;

namespace UniversalBlazor.ElementUI.TabComponent
{
    public partial class TabPanel
    {
        [Parameter]
        public EventCallback<ChangingEventArgs<TabPanel>> OnTabPanelChanging { get; set; }

        public override string ToString()
        {
            return Name;
        }

        //protected async Task OnInternalTabCloseAsync(MouseEventArgs e)
        //{
        //    RequireRender = true;
        //    await TabContainer.CloseTabAsync(this);
        //}

        [Parameter]
        public bool IsActive { get; set; }
        public ElementReference Element { get; set; }

        [CascadingParameter]
        public Tab Tab { get; set; }

        //protected async Task Activate(MouseEventArgs e)
        //{
        //    IsActive = await TabContainer.SetActivateTabAsync(this);
        //}

        public void Activate()
        {
            Tab.headerSizeUpdated = false;
            IsActive = true;
            Tab.Activate(this);
        }

        [Parameter]
        public string Name { get; set; }

        internal int Index { get; set; }

        [Parameter]
        public string Title { get; set; }

        [Parameter]
        public RenderFragment ChildContent { get; set; }

        [Parameter]
        public bool IsClosable { get; set; }

        protected override Task OnInitializedAsync()
        {
            Index = Tab.tabIndex++;
            if (string.IsNullOrWhiteSpace(Name))
            {
                Name = "tab_" + Index;
            }
            if (!Tab.Exists(Name))
            {
                Tab.AddTab(this);
            }
            return Task.CompletedTask;
        }

        internal void DeActivate()
        {
            IsActive = false;
        }
    }
}
